import useStore from "@/store";
import { getToken } from "@/utils/storage";

import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw,
} from "vue-router";
import Layout from "@/pages/Layout/index.vue";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "/category",
        component: () => import("@/pages/Category/index.vue"),
      },
      {
        path: "/home",
        component: () => import("@/pages/Home/index.vue"),
      },
    ],
  },
  {
    path: "/goodsDetail",
    component: () => import("@/pages/GoodsDetail/index.vue"),
  },
  {
    path: "/test",
    component: () => import("@/pages/test/index.vue"),
  },
  {
    path: "/404",
    component: () => import("@/pages/404/index.vue"),
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let store: any = null;
router.beforeEach(function (to, from, next) {
  if (!store) {
    store = useStore(); //pinia只能在运行时使用
  }

  if (getToken() && !store.user.userInfo.id) {
    store.user.validToken();
  }
  next();
});
export default router;
